package com.dxy;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class PoiUtil {

    public static void writeTitle(XWPFDocument doc, String title){
        XWPFParagraph content = doc.createParagraph();
        content.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun titleRun = content.createRun();
        titleRun.setText(title);
        titleRun.setBold(true);
        titleRun.setFontSize(14);
    }

    public static void writeTable(XWPFDocument doc, List<ColumnMeta> list){
        XWPFTable table = doc.createTable(list.size()+1, 9);

        // 设置表格宽度
        table.setWidth("100%");

        // 设置表头背景色
        XWPFTableRow headerRow = table.getRow(0);
        for (XWPFTableCell cell : headerRow.getTableCells()) {
            CTShd ctShd = cell.getCTTc().addNewTcPr().addNewShd();
            ctShd.setFill("E7E6E6"); // 灰色
            // 设置段落居中
            XWPFParagraph paragraph = cell.getParagraphs().get(0);
            paragraph.setAlignment(ParagraphAlignment.CENTER);
        }
        headerRow.getCell(0).setText("编号");
        headerRow.getCell(1).setText("名称");
        headerRow.getCell(2).setText("数据类型");
        headerRow.getCell(3).setText("长度");
        headerRow.getCell(4).setText("小数位");
        headerRow.getCell(5).setText("允许空值");
        headerRow.getCell(6).setText("主键");
        headerRow.getCell(7).setText("默认值");
        headerRow.getCell(8).setText("说明");

        for (int i = 0; i < list.size(); i++) {
            ColumnMeta meta = list.get(i);
            XWPFTableRow row = table.getRow(i+1);
            // row.getCell(0).setText(String.valueOf(i+1));
            // row.getCell(1).setText(meta.getColumnName());
            // row.getCell(2).setText(meta.getDataType());
            // row.getCell(3).setText(meta.getCharMaxLength());
            // row.getCell(4).setText(meta.getNumberScale());
            // row.getCell(5).setText(meta.getNullable());
            // row.getCell(6).setText(meta.getPrimaryKey());
            // row.getCell(7).setText(meta.getDefaultValue());
            // row.getCell(8).setText(meta.getColumnDesc());
            for (int j = 0; j < 9; j++) {
                XWPFTableCell cell = row.getCell(j);
                XWPFParagraph paragraph = cell.getParagraphs().get(0);
                paragraph.setAlignment(ParagraphAlignment.CENTER);

                // 设置单元格文本
                switch (j) {
                    case 0:
                        cell.setText(String.valueOf(i + 1));
                        break;
                    case 1:
                        cell.setText(meta.getColumnName());
                        break;
                    case 2:
                        cell.setText(meta.getDataType());
                        break;
                    case 3:
                        cell.setText(meta.getCharMaxLength());
                        break;
                    case 4:
                        cell.setText(meta.getNumberScale());
                        break;
                    case 5:
                        cell.setText(meta.getNullable());
                        break;
                    case 6:
                        cell.setText(meta.getPrimaryKey());
                        break;
                    case 7:
                        cell.setText(meta.getDefaultValue());
                        break;
                    case 8:
                        cell.setText(meta.getColumnDesc());
                        break;
                }
            }
        }
    }

    public static void main(String[] args) {
        try(XWPFDocument doc = new XWPFDocument()) {

            List<ColumnMeta> list = new ArrayList<>();
            list.add(new ColumnMeta().setColumnName("ID")
                    .setDataType("INT")
                    .setCharMaxLength("")
                    .setNumberScale("")
                    .setNullable("否")
                    .setPrimaryKey("√")
                    .setDefaultValue("")
                    .setColumnDesc("主键id")
            );
            list.add(new ColumnMeta().setColumnName("NAME")
                    .setDataType("VARCHAR")
                    .setCharMaxLength("64")
                    .setNumberScale("")
                    .setNullable("是")
                    .setPrimaryKey("")
                    .setDefaultValue("")
                    .setColumnDesc("姓名"));
            writeTitle(doc, "T_USER");
            writeTable(doc,list);


            try (FileOutputStream out = new FileOutputStream("xxx.docx")) {
                doc.write(out);
                System.out.println("文档已生成: xxx.docx");
            }

        }catch (Exception e){
            System.out.println("发生异常：");
            e.printStackTrace();
        }
    }
}
